package com.fh.service.transaction;

import com.fh.dto.TranRequest;
import com.fh.dto.TranResponse;
import com.fh.entity.HttpResult;
import com.fh.exception.ServiceException;
import com.fh.service.ServiceCheck.CheckHandler;
import com.fh.service.airsys.airlineemployee.AirlineEmployeeService;
import com.fh.service.airsys.airlinerummery.AirlineRummeryService;
import com.fh.service.airsys.syscreditaccount.SysCreditAccountService;
import com.fh.service.airsys.transactiondetails.TransactionDetailsService;
import com.fh.util.DateUtil;
import com.fh.util.Logger;
import com.fh.util.PageData;
import com.fh.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * Created by tugou on 2017/9/24.
 */
@Service
public class OnLineTranServiceImpl implements OnLineTranService {

    protected Logger logger = Logger.getLogger(this.getClass());


    @Autowired
    @Qualifier("infoCheck")
    CheckHandler infoCheck;

    @Autowired
    @Qualifier("accBalanceCheck")
    CheckHandler accBalanceCheck;
    @Autowired
    TransactionDetailsService transactionDetailsService;
    @Autowired
    SysCreditAccountService sysCreditAccountService;
    @Autowired
    AirlineEmployeeService airlineEmployeeService;
    @Autowired
    AirlineRummeryService airlineRummeryService;

    public HttpResult tranExce(TranRequest request){
        try {
            PageData pd = new PageData();
            pd.put("EI_EMINFO",request.getEmpId());
            pd.put("AIRLINEINFO_ID",request.getOrgId());
            pd.put("tranAmt",request.getTranAmt());
            infoCheck.handleRequest(pd);//用户信息检查
            //accBalanceCheck.handleRequest(pd);//账户余额检查
            PageData empData = airlineEmployeeService.findById(pd);
            pd.put("SA_ACC",empData.getString("SA_ACC"));
            PageData accData = sysCreditAccountService.findById(pd);
            Integer balance = (Integer) empData.get("SA_BALANCE");
            Integer afterBalance = balance + Integer.valueOf(request.getTranAmt());
            String tranDateTime = DateUtil.getTime();
            accData.put("SA_BALANCE",afterBalance.toString());
            accData.put("SA_LASTTRADTIME", tranDateTime);
            sysCreditAccountService.edit(accData);//余额
            PageData tranPage = new PageData();
            String tson = UuidUtil.get32UUID();
            if(request.getTson() != null){
                tson = request.getTson();
            }
            tranPage.put("TRANSACTIONDETAILS_ID",tson);
            tranPage.put("TD_SN",UuidUtil.get32UUID());
            tranPage.put("AL_CODE",request.getOrgId());
            tranPage.put("EI_CODE",empData.get("AIRLINEEMPLOYEE_ID"));
            tranPage.put("SA_ACC",empData.getString("SA_ACC"));
            tranPage.put("TD_TIME",tranDateTime);
            tranPage.put("TRAN_TYPE",request.getTranType());
            tranPage.put("TD_AMOUNT",Math.abs(request.getTranAmt()));
            tranPage.put("RI_CODE",request.getRummeryId());
            tranPage.put("TD_STATE","00000");
            tranPage.put("BALANCEAFERT",afterBalance.toString());
            tranPage.put("BALANCEBEFORE",balance.toString());
            transactionDetailsService.save(tranPage);
            pd.put("AIRLINERUMMERY_ID",request.getRummeryId());
            PageData rummeryData = airlineRummeryService.findById(pd);
            TranResponse tranResponse = new TranResponse();
            tranResponse.setBalanceAfert(afterBalance.toString());
            tranResponse.setBalanceBefore(balance.toString());
            tranResponse.setTranDateTime(tranDateTime);
            tranResponse.setTson(tson);
            tranResponse.setTranAmt(String.valueOf(Math.abs(request.getTranAmt())));
            tranResponse.setRummeryEnName(rummeryData.getString("RI_ENAME"));
            tranResponse.setRummeryName(rummeryData.getString("RI_NAME"));
            tranResponse.setBalanceBefore(balance.toString());
            tranResponse.setBalanceAfert(afterBalance.toString());
            return HttpResult.OK(tranResponse);
        }catch (ServiceException e){
            logger.info(e.getMessage());
            return HttpResult.ERROR(String.valueOf(e.getCode()),e.getMsg());
        }
        catch (Exception e) {
            logger.info(e.getMessage());
            return HttpResult.ERROR("55555",e.getMessage());
        }
    }
}
